package com.example.dishit.view.main;

import java.util.List;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.example.dishit.R;
import com.example.dishit.controller.main.MainController;
import com.example.dishit.model.dish.DishData;
import com.example.dishit.view.createdish.CreateDishActivity;
import com.example.dishit.view.editpref.EditPrefActivity;
import com.example.dishit.view.friendmaint.FriendMaintActivity;

public class MainActivity extends Activity implements IMainView {
    private ReceivedDishesFragment receivedDishesFragment;
    private SentDishesFragment sentDishesFragment;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        setTitle(MainController.THIS_USER_ID);
        
        // setup action bar for tabs
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        receivedDishesFragment = new ReceivedDishesFragment();
        Tab tab = actionBar.newTab()
                    .setText("Received")
                    .setTabListener(new MainTabListener(receivedDishesFragment, "Received"));
        actionBar.addTab(tab);
        
        sentDishesFragment = new SentDishesFragment();
        tab = actionBar.newTab()
                    .setText("Sent")
                    .setTabListener(new MainTabListener(sentDishesFragment, "Sent"));
        actionBar.addTab(tab);
        
        // Set MainController's view to this.
        MainController.instance().setView(this);
    }
    
    private static class MainTabListener implements TabListener {
        private boolean initialized;
        private final Fragment fragment;
        private final String tag;
        
        public MainTabListener(final Fragment fragment, final String tag) {
            this.fragment = fragment;
            this.tag = tag;
        }
        
        public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
            // Simply refresh.
            onTabUnselected(arg0, arg1);
            onTabSelected(arg0, arg1);
        }
        
        public void onTabSelected(Tab arg0, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            if(!initialized) {
                // If not, instantiate and add it to the activity
                ft.add(android.R.id.content, fragment, tag);
                initialized = true;
            } else {
                // If it exists, simply attach it in order to show it
                ft.attach(fragment);
            }
        }
        
        public void onTabUnselected(Tab arg0, FragmentTransaction ft) {
            if(fragment != null) {
                // Detach the fragment, because another one is being attached
                ft.detach(fragment);
            }
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.find_restaurant_button:
                MainController.instance().findRestaurant();
                return true;
            case R.id.create_dish_button:
                MainController.instance().transition(new Bundle(), CreateDishActivity.class);
                return true;
            case R.id.edit_followers_button:
                MainController.instance().transition(new Bundle(), FriendMaintActivity.class);
                return true;
            case R.id.preferences_button:
                MainController.instance().transition(new Bundle(), EditPrefActivity.class);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void addReceivedDish(DishData dish) {
        receivedDishesFragment.addDish(dish);
    }
    
    public void addSentDish(DishData dish) {
        sentDishesFragment.addDish(dish);
    }
    
    public void setReceivedDishes(List<DishData> dishes) {
        receivedDishesFragment.setDishes(dishes);
    }
    
    public void setSentDishes(List<DishData> dishes) {
        sentDishesFragment.setDishes(dishes);
    }
}
